<?php

namespace libs;

class init
{
    static public function run()
    {
        include __DIR__.DS."constants.php";
        include __DIR__.DS."functions.php";

        set_exception_handler(__NAMESPACE__.'\init::kyoException');
        set_error_handler(__NAMESPACE__.'\init::kyoError');
        spl_autoload_register(__NAMESPACE__."\init::autoload");
    }

    static public function autoload($class)
    {
        // echo "class: ", $class, "<br />";
        $classfile = str_replace('\\', DS, $class).".class.php";
        // echo ROOT.$classfile, PHP_EOL;
        if (file_exists($classfile))
            include ROOT.$classfile;
    }

    static public function halt($str = null)
    {
        is_string($str) and fprintf(STDERR, $str);
        exit(1);
    }

    static public function kyoError($no, $str, $file, $line)
    {
        if (!DEBUG)
            init::halt(0);

        $html = $file.' + '.$line.':'.PHP_EOL;
       $html .= '    ('.$no.')'.$str.PHP_EOL;

        init::halt($html);
    }

    static public function kyoException($e)
    {
        $html = '---- kyo Exception: '. $e->getMessage().PHP_EOL;

        init::halt($html);
    }
}
